package fy.uap.manager;

import java.sql.Connection;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.ModelAndView;

import fy.config.FyConfig;
import fy.form.listen.bean.Node;
import fy.form.listen.set.OA_UAPAccountContrast;
import fy.form.oa.dao.OAOrgAccountDao;
import fy.uap.dao.FillSupplierInfoSelectDao;
import fy.uap.dao.TypeTreeDao;
import fy.util.DBUtil;

public class InitJSP {

	private static Log log = LogFactory.getLog(InitJSP.class);

	private static String companyOption;

	public static void initFillProviderImportJSP(ModelAndView modelAndView,String codes) {
		Connection conn = null;
		try {
			conn = DBUtil.connect(FyConfig.getUAP_DB_CONFIG());
			TypeTreeDao ttd = new TypeTreeDao();
			ttd.setConn(conn);
			ttd.queryType(codes);

			modelAndView.addObject("treeXML", ttd.getRoot().toXMLElement()
					.asXML());

			modelAndView.addObject("typeJSON", ttd.getJsonObject().toString());

			FillSupplierInfoSelectDao fsd = new FillSupplierInfoSelectDao();

			fsd.querySelectOption(conn);

			modelAndView.addObject("cvenbankSelect", fsd.getCvenbankSelect());
			modelAndView.addObject("couttypeSelect", fsd.getCouttypeSelect());
			modelAndView.addObject("cmoneytypeSelect",
					fsd.getCmoneytypeSelect());
			modelAndView.addObject("companyTypeSelect",
					fsd.getCompanyTypeSelect());

			modelAndView.addObject("companyTreeXML",
					OAOrgAccountDao.findAccountTree());
			log.info("companyTreeXML----->>>");
			modelAndView.addObject("aptitudeClassJSON",
					fsd.getAptitudeClassSelect());

		} catch (Exception e) {
			log.error("初始化输入供应商导入表界面异常", e);
			e.printStackTrace();
		} finally {
			DBUtil.close(conn);
		}
	}

	public static void initMergerProviderJSP(ModelAndView modelAndView) {
		Connection conn = null;
		try {
			conn = DBUtil.connect(FyConfig.getUAP_DB_CONFIG());

			FillSupplierInfoSelectDao fsd = new FillSupplierInfoSelectDao();

			fsd.querySelectOption(conn);

			modelAndView.addObject("cvenbankSelect", fsd.getCvenbankSelect());
			modelAndView.addObject("couttypeSelect", fsd.getCouttypeSelect());
			modelAndView.addObject("cmoneytypeSelect",
					fsd.getCmoneytypeSelect());

			modelAndView.addObject("companyTypeSelect",
					fsd.getCompanyTypeSelect());
			modelAndView.addObject("aptitudeClassJSON",
					fsd.getAptitudeClassSelect());

		} catch (Exception e) {
			log.error("初始化输入供应商更名合并表界面异常", e);
		} finally {
			DBUtil.close(conn);
		}
		if (companyOption == null) {
			List<Node> accountList = OA_UAPAccountContrast.getOaAccountList();

			StringBuilder sb = new StringBuilder();
			for (int i = 0, size = accountList.size(); i < size; i++) {
				sb.append("<option value='").append(accountList.get(i).getId())
						.append("'>").append(accountList.get(i).getText())
						.append("</option>");
			}
			companyOption = sb.toString();
		}
		modelAndView.addObject("company", companyOption);
	}
	
	public static void initMergerProviderInfoJSP(ModelAndView modelAndView) {
		Connection conn = null;
		try {
			conn = DBUtil.connect(FyConfig.getUAP_DB_CONFIG());


			FillSupplierInfoSelectDao fsd = new FillSupplierInfoSelectDao();

			fsd.querySelectOption(conn);

			modelAndView.addObject("cvenbankSelect", fsd.getCvenbankSelect());
			modelAndView.addObject("couttypeSelect", fsd.getCouttypeSelect());
			modelAndView.addObject("cmoneytypeSelect",
					fsd.getCmoneytypeSelect());

			modelAndView.addObject("companyTypeSelect",
					fsd.getCompanyTypeSelect());
			modelAndView.addObject("aptitudeClassJSON",
					fsd.getAptitudeClassSelect());

		} catch (Exception e) {
			log.error("初始化输入供应商信息变更表界面异常", e);
		} finally {
			DBUtil.close(conn);
		}
	}

	

	public static void initInvalidProviderJSP(ModelAndView modelAndView,String codes) {
		if (companyOption == null) {
			List<fy.form.listen.bean.Node> accountList = OA_UAPAccountContrast.getOaAccountList();

			StringBuilder sb = new StringBuilder();
			for (int i = 0, size = accountList.size(); i < size; i++) {
				sb.append("<option value='").append(accountList.get(i).getId())
						.append("'>").append(accountList.get(i).getText())
						.append("</option>");
			}
			companyOption = sb.toString();
		}
		modelAndView.addObject("company", companyOption);
		Connection conn = null;
		try {
			conn = DBUtil.connect(FyConfig.getUAP_DB_CONFIG());

			TypeTreeDao ttd = new TypeTreeDao();
			ttd.setConn(conn);
			ttd.queryType(codes);
			modelAndView.addObject("treeXML", ttd.getRoot().toXMLElement(true)
					.asXML());

			modelAndView.addObject("typeJSON", ttd.getJsonObject().toString());
		} catch (Exception e) {
			log.error("初始化输入供应商导出表界面异常", e);
		} finally {
			DBUtil.close(conn);
		}
	}

	// public static void initCaigouhetonghuiqianJSP(ModelAndView modelAndView)
	// {
	// Connection conn = null;
	// try {
	// conn = DBUtil.connect(OA2UAPConf.UAPDRIVERCLASSNAME,
	// OA2UAPConf.UAPURL, OA2UAPConf.UAPUSERNAME,
	// OA2UAPConf.UAPPASSWORD);
	// TypeTreeDao ttd = new TypeTreeDao();
	// ttd.setConn(conn);
	// ttd.queryType();
	//
	// modelAndView.addObject("treeXML", ttd.getRoot().toXMLElement(true)
	// .asXML());
	//
	// modelAndView.addObject("typeJSON", ttd.getJsonObject().toString());
	// } catch (Exception e) {
	// log.error("初始化输入供应商导入表界面异常", e);
	// } finally {
	// DBUtil.close(conn);
	// }
	// }

	// insert into myTest m1 ( m1.id, m1.text) select '2', 'test222' from dual
	// where not exists( select * from myTest m where m.id = '2')
}
